﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace WmsApp.Security
{
    public partial class FormChangePass : DevExpress.XtraEditors.XtraForm
    {
        private bool isChange = false;
        private string userID = String.Empty;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="isChange">是否是修改，如果修改，则需要显示原始密码输入框</param>
        /// <param name="userID">用户ID</param>
        public FormChangePass(bool isChange,string userID)
        {
            InitializeComponent();
            this.isChange = isChange;
            this.userID = userID;
            InitializeForm();
        }

        private void InitializeForm()
        {
            if(!isChange)//如果是设置密码，则直接设置
            {
                //this.labelControl1.Visible = false;
                //this.textEditOld.Visible = false;
                this.textEditOld.Enabled = false;
                var client = Factory.WmsServiceFactory.GetServiceClient();
                this.textEditOld.Text = client.GetSystemUser(userID).PASSWORD;
            }
        }

        private bool ValidatePassword()
        {
            if(textEditNew1.Text != textEditNew2.Text || String.IsNullOrEmpty(textEditNew1.Text))
            {
                //Exceptions.ExceptionHandler.ShowException("两次新密码输入不一致！或者为空！");
                XtraMessageBox.Show("两次新密码输入不一致！或者为空！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            return true;
        }

        private void CmdOk_Click(object sender, EventArgs e)
        {
            try
            {
                bool result = ValidatePassword();
                if (!result)
                {
                    return;
                }
                string msg = String.Empty;
                if (!isChange)
                {
                    var client = Factory.WmsServiceFactory.GetServiceClient();
                    msg = client.SetPassword(userID, this.textEditNew1.Text);
                }
                else
                {
                    var client = Factory.WmsServiceFactory.GetServiceClient();
                    msg = client.ChangePassword(UserSession.CurrentUser.USERID, this.textEditOld.Text, this.textEditNew1.Text);
                }
                XtraMessageBox.Show(msg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                this.Close();
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }
    }
}